home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’97
/
Alias Sizer
/
source code
/
INIT.c
< prev
Wrap
C/C++ Source or Header
|
1997-06-28
|
4KB
|
171 lines
//AliasSizer
//MacHack 0x0C
//Copyright ©1997 Barry J. Semo. All Rights Reserved
//barrys@netcom.com
#include "A4Stuff.h"
#include "SetupA4.h"
//typedefs
typedef struct SizeResource{
short flags;
long preferred;
long minimum;
}SizeRsrc, *SizeRsrcPtr, **SizeRsrcHandle;
typedef pascal short (*HOpenResFileProc)(short vRefNum, long dirID,
ConstStr255Param fileName, SInt8 permission);
typedef pascal Handle (*Get1ResourceProc)(ResType theType, short theID);
//prototypes
pascal short HOpenResFilePatch(short vRefNum, long dirID,
ConstStr255Param fileName, SInt8 permission);
pascal Handle Get1ResourcePatch(ResType theType, short theID);
pascal Handle MyGet1ResourceAsm (ResType resType, short resID);
//original trap addresses
Get1ResourceProc gOldGet1ResourceAddr;
HOpenResFileProc gOldHOpenResFileAddr;
//global variables
SizeRsrc gSizeResource;
Boolean gCheckForSize, gIntercept;
void main(void)
{
long oldA4;
Handle h;
// a4 world
oldA4 = SetCurrentA4();
RememberA4();
// detach init resource
h = Get1Resource('INIT', 0);
if (h) DetachResource(h);
//init
gSizeResource.flags = 0;
gSizeResource.preferred = 0L;
gSizeResource.minimum = 0L;
gCheckForSize = false;
gIntercept = false;
// patch
gOldHOpenResFileAddr = (HOpenResFileProc)GetToolTrapAddress(_HOpenResFile);
SetToolTrapAddress((UniversalProcPtr)HOpenResFilePatch, _HOpenResFile);
gOldGet1ResourceAddr = (Get1ResourceProc)GetToolTrapAddress(_Get1Resource);
SetToolTrapAddress((UniversalProcPtr)MyGet1ResourceAsm, _Get1Resource);
//restore
SetA4(oldA4);
}
//HOpenResFile patch - catch opening an alias to an application
//then get a SIZE resource out of it, save it for use in a Get1Resource
//call, and set a flag to let the Get1Resource patch know
extern pascal short HOpenResFilePatch(short vRefNum, long dirID,
ConstStr255Param fileName, SInt8 permission)
{
#pragma unused (permission)
long oldA4;
FInfo theInfo;
OSErr err;
short refNum;
oldA4 = SetUpA4();
//check file type for 'adrp' or, an alias to an application
//other alias types we dont care
err = HGetFInfo(vRefNum, dirID, fileName, &theInfo);
if ((err == noErr) && (theInfo.fdType == 'adrp'))
{
gCheckForSize = true;//its the right kind of alias, so set checksize flag
}
//do the real hopenresfile
refNum = gOldHOpenResFileAddr(vRefNum, dirID, fileName, permission);
//check to see if flag set for sizechecking and the file opened
// successfully
if (gCheckForSize && (refNum != -1))
{
SizeRsrcHandle sizeHndl;
gCheckForSize = false;//reset flag
gIntercept = false;//clear for next get1resource call
//get size resource out of alias
sizeHndl = (SizeRsrcHandle)Get1Resource('SIZE', -1);
if (sizeHndl)
{
//found resource so load it and save info
LoadResource((Handle)sizeHndl);//load it cuz resload is false
//save for later
gSizeResource = **sizeHndl;
gIntercept = true;
//SetHFSDispatchFlag();
}
}
RestoreA4(oldA4);
return refNum;
}
//get1resource patch from launch with size sample code from apple
//tool chest may 97 cdrom
//Get1Resource patch will stuff a SIZE resource when a flag is set
//in HOpenResFile, and its getting a SIZE resource
static pascal Handle MyGet1ResourceC (ResType resType, short resID)
{
long oldA4 = SetUpA4();
//call real one
Handle result = gOldGet1ResourceAddr(resType,resID);
//wait for valid size resource comin back (might not be first
//one to try so the intercept flag is inside
if ( (resType == 'SIZE') && !ResError ( ) && result )
{
if (gIntercept)
{
gIntercept = false;//clear flag until nexttime
//blast in my size resource
if (gSizeResource.preferred)
**(SizeRsrcHandle)result = gSizeResource;
}
}
RestoreA4 (oldA4);
return result;
}
static asm pascal Handle MyGet1ResourceAsm (ResType resType, short resID)
{
move.l a1,-(a7) // save reg
move.l d1,-(a7) // save reg
move.l d2,-(a7) // save reg
subq.l #4,a7 // room for result
move.l 22(a7),-(a7) // push copy of resType
move.w 24(a7),-(a7) // push copy of resID
jsr MyGet1ResourceC // do work
move.l (a7)+,22(a7) // copy and pop result
move.l (a7)+,d2 // restore reg
move.l (a7)+,d1 // restore reg
move.l (a7)+,a1 // restore reg
move.l (a7)+,a0 // ready return thru A0
addq.l #6,a7 // pop args
jmp (a0) // phone home
}